<?php /**
*An object to watch all other objects in the system.
*Ensures objects are not duplicated and provides a queue mechanism for inserting
*or removing items for database to ensure operation is only performed once.
*An object is added to a queue and cannot be added to the queue a second time
*though it can be removed.
*
*@package Business
*@author Garry Willits
*@copyright 2005
*@version 1.1
*/
class ObjectWatcher{
/**
*@var DomainObjectCollection
*/
private $all;
/**
*@var DomainObjectCollection
*/
private $new;
/**
*@var DomainObjectCollection
*/
private $changed;
/**
*@var DomainObjectCollection
*/
private $unchanged;
/**
*@var DomainObjectCollection
*/
private $delete;
/**
*@var ObjectWatcher
*/
private static $instance;
/**
*Singleton Pattern
*/
private function __construct(){
$this->new = new DomainObjectCollection();
$this->all = new DomainObjectCollection();
$this->changed = new DomainObjectCollection();
$this->unchanged = new DomainObjectCollection();
$this->delete = new DomainObjectCollection();
}
/**
*Singleton Pattern
*@return ObjectWatcher
*/
public static function getInstance(  ){
    if( !self::$instance ){
    self::$instance = new ObjectWatcher( );
  }
return self::$instance;
}
/**
*add an Object to changed list
*/
public static function addChanged( DomainObject $object ){
$instance = self::getInstance();
    if( !$instance->changed->contains( $object ) ){
    $instance->changed->addElement( $object );
    }
}
/**
*add an Object to unChanged list
*@param DomainObject
*/
public static function addUnchanged( DomainObject $object ){
$instance = self::getInstance();
$instance->changed->removeElement( $object );
    if( !$instance->unchanged->contains( $object ) ){
    $instance->unchanged->addElement( $object );
    }
}
/**
*add an Object to new list
*@param DomainObject
*/
public static function addNew( DomainObject $object ){
	$instance = self::getInstance();
    if( !$instance->new->contains( $object ) ){
    	$instance->new->addElement( $object );
    }

}
/**
*add an Object to delete list
*@param DomainObject
*/
public static function addDelete( DomainObject $object ){
$instance = self::getInstance();
    if( !$instance->delete->contains( $object ) ){
    $instance->delete->addElement( $object );
    }
}
/**
*Process all queues
*/
public static function performOperations(){
$instance = self::getInstance();
    foreach( $instance->new as $new  ){
		$new->mapper()->insert( $new );
    }   
    foreach( $instance->changed as $changed ){
    	$changed->mapper()->update( $changed );
    }
    foreach( $instance->delete as $delete ){
    $delete->mapper()->delete( $delete );
  }

}
/**
*Add an ID to the register with object that Mapper objects can check to see if object **exists.
*This is used to avoid duplication in the system.
*/
public static function add( DomainObject $object, $id ){
	$instance = self::getInstance();
	$replace = $instance->all[ $id ];
	    if( !$replace ){
	    $instance->all[ $id ] = $object;
	    }
	    else {
	    $object = $replace;
	  }
	return $object;
	}
}

?>
